package com.chuangke.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangke.admin.data.builder.UserDataBuilder;
import com.chuangke.common.data.builder.DataBuildConfig;
import com.chuangke.common.data.builder.DataBuilder;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.idgen.IdManager;
import com.chuangke.file.dao.SysFileShareMapper;
import com.chuangke.file.model.SysFileShare;
import com.chuangke.file.service.SysFileShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 文档链接分享Service业务层处理
 * 
 * @author chuangke
 * @date "2020-09-08"
 */
@Service
public class SysFileShareServiceImpl extends ServiceImpl<SysFileShareMapper, SysFileShare> implements SysFileShareService {
	@Autowired
    private SysFileShareMapper sysFileShareMapper;


    /**
     * 查询文档链接分享列表
     * 
     * @param sysFileShare 文档链接分享
     * @return 文档链接分享
     */
    @Override
    public List<SysFileShare> selectSysFileShareList(SysFileShare sysFileShare){
    	QueryWrapper<SysFileShare> queryWrapper=new QueryWrapper<>();
    	String dbankId=sysFileShare.getDbankId();
    	if(dbankId!=null&& !dbankId.trim().isEmpty()) {
    		queryWrapper.lambda().eq(SysFileShare::getDbankId, dbankId);
    	}
	    List<SysFileShare> resultList=sysFileShareMapper.selectList(queryWrapper);
	    DataBuilder.of(resultList)
			.appendBuilder(new UserDataBuilder(), new DataBuildConfig("createdBy", "createdBy"),
					new DataBuildConfig("updatedBy", "updatedBy")).build();
	        return resultList;
    }

    /**
     * 新增文档链接分享
     * 
     * @param sysFileShare 文档链接分享
     * @return 结果
     */
    @Override
	@Transactional(rollbackFor = Exception.class)
     public boolean saveOrUpdateSysFileShare(SysFileShare sysFileShare){
    	String id=sysFileShare.getId();
    	if(id==null|| id.trim().isEmpty()){
    		sysFileShare.setId(IdManager.nextId());
    	}
        return this.saveOrUpdate(sysFileShare);
    }

    /**
     * 批量删除文档链接分享
     * 
     * @param ids 需要删除的文档链接分享ID
     * @return 结果
     */
    @Override
    public int deleteSysFileShareByIds(List<String> ids){
    	return sysFileShareMapper.deleteBatchIds(ids);
    }

	@Override
	public IPage<SysFileShare> page(CkPageQuery pageQuery) {
		QueryWrapper<SysFileShare> queryWrapper = new QueryWrapper<>();
		return sysFileShareMapper.selectPage(pageQuery.getPage(), queryWrapper);
	}

}
